﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.ServiceModel;
using CMS.Model;
using CMS.Contracts;
namespace CMS.Presentation
{
    public partial class CourseList : System.Web.UI.Page
    {//declare componnents in web page
        private ContentPlaceHolder _mpContentPlaceHolder;
        private TextBox _courseName;
        private RadioButtonList _courseStatusList;
        private int _courseStatus = -1;
        private DropDownList _teacherName;
        private DropDownList _courseLevel;
        private GridView _courseListView;
        private static CMS.Model.Condition _condition;
        public static List<CMS.Model.CourseList> _testList;
        public int _pageIndex = 1;
        public int _pageSize = 5;
        public int _rowCount;
        public int _pageCount = 1;
        /// <summary>
        /// Page Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //get componnents in master page 
            _mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl("MainContent");
            _courseName = (TextBox)_mpContentPlaceHolder.FindControl("CourseName");
            _teacherName = (DropDownList)_mpContentPlaceHolder.FindControl("TeacherName");
            _courseLevel = (DropDownList)_mpContentPlaceHolder.FindControl("CourseLevel");
            _courseStatusList = (RadioButtonList)_mpContentPlaceHolder.FindControl("CourseStatusRadionButtonList");
            _courseListView = (GridView)_mpContentPlaceHolder.FindControl("CourseListView");

            if (!Page.IsPostBack)
            {
                //bind teacher name in dropdownlist
                bindDropTeacherName();

                //bind Course Level in dropdownlist
                bindDropLevelName();

                //set default column to sort
                ViewState["SortOrder"] = "CourseId";
                ViewState["OrderDirection"] = "ASC";
                ViewState["CurrentPage"] = _pageIndex;
            }
        }
        #region
        /// <summary>
        /// bind LevelName in Dropdownlist
        /// </summary>
        private void bindDropLevelName()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("CMSClient"))
            {
                IProjectBService iProjectBService = channelFactory.CreateChannel();
                using (iProjectBService as IDisposable)
                {
                    this._courseLevel.DataSource = iProjectBService.GetAllLevels();
                    this._courseLevel.DataTextField = "LevelName";
                    this._courseLevel.DataValueField = "LevelId";
                    this._courseLevel.DataBind();
                }
            }
            this._courseLevel.Items.Insert(0, "--Choose Level--");
            this._courseLevel.SelectedIndex = 0;


        }

        /// <summary>
        /// bind TeacherName in DropdownList
        /// </summary>
        private void bindDropTeacherName()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("CMSClient"))
            {
                IProjectBService iProjectBService = channelFactory.CreateChannel();
                using (iProjectBService as IDisposable)
                {
                    this._teacherName.DataSource = iProjectBService.GetAllTeacher();
                    this._teacherName.DataTextField = "TeacherName";
                    this._teacherName.DataValueField = "TeacherId";
                    this._teacherName.DataBind();
                }
            }

            this._teacherName.Items.Insert(0, "--Choose Teacher--");
            this._teacherName.SelectedIndex = 0;
        }
        #endregion
        /// <summary>
        /// Search Button Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SearchBtnClick(object sender, EventArgs e)
        {

            if (_courseStatusList.SelectedValue == "True")
            {
                _courseStatus = 1;
            }
            else if (_courseStatusList.SelectedValue == "False")
            {
                _courseStatus = 0;
            }
            string TeacherName = "";
            if (this.TeacherName.SelectedIndex != 0)
            {
                TeacherName = this._teacherName.SelectedItem.Text.Trim();
            }
            string LevelName = "";
            if (this.TeacherName.SelectedIndex != 0)
            {
                LevelName = this._courseLevel.SelectedItem.Text.Trim();
            }
            string CourseNameInput = this._courseName.Text.Trim();

            _condition = new CMS.Model.Condition(CourseNameInput, _courseStatus, TeacherName, LevelName);
            bindReport();
        }

        /// <summary>
        /// bind CourseList Report to Gridview
        /// </summary>
        private void bindReport()
        {
            //pass condition
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("CMSClient"))
            {
                IProjectBService iProjectBService = channelFactory.CreateChannel();
                using (iProjectBService as IDisposable)
                {
                    _condition.OrderDirection = ViewState["OrderDirection"].ToString();
                    _condition.SortExpression = ViewState["SortOrder"].ToString();

                    _testList = new List<CMS.Model.CourseList>();
                    _testList = iProjectBService.GenerateCourseReport(_condition,_pageIndex,_pageSize, out _rowCount);
                }
            }
            //find no record ?
            if (_testList != null)
            {
                if (_testList.Count > 0)
                {
                    
                    errMsg.Visible = false;
                }
                else
                {
                    errMsg.Visible = true;
                }
            }

            //sort list
            var type = (Presentation.CourseListcomparer.CompareType)Enum.Parse(typeof(Presentation.CourseListcomparer.CompareType), ViewState["SortOrder"].ToString().Trim());

            Presentation.CourseListcomparer.CourseListComparer courselistComparer = new Presentation.CourseListcomparer.CourseListComparer(type);

            _testList.Sort(courselistComparer);
            if (ViewState["OrderDirection"].ToString() == "Desc")
            {
                _testList.Reverse();
            }
            //bind list
            _courseListView.DataSource = _testList;
            _courseListView.DataBind();
        }

        /// <summary>
        /// Gridview Paging
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CourseListView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //before paging
            _courseListView.PageIndex = e.NewPageIndex;
            bindReport();
        }

        /// <summary>
        /// GridView Sorting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CourseList_Sorting(object sender, GridViewSortEventArgs e)
        {
            String sortExpression = e.SortExpression;
            if (ViewState["SortOrder"].ToString() == sortExpression)
            {
                if (ViewState["OrderDirection"].ToString() == "Desc")
                {
                    ViewState["OrderDirection"] = "ASC";
                }
                else
                {
                    ViewState["OrderDirection"] = "Desc";
                }
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
            }
            bindReport();
        }

    }
}
